package com.tony.blog.server.controller.admin;

import com.google.common.collect.Maps;
import com.tony.blog.api.response.BaseResponse;
import com.tony.blog.api.response.StatusCode;
import com.tony.blog.api.utils.PageUtil;
import com.tony.blog.server.annotation.LogAnnotation;
import com.tony.blog.server.controller.AbstractController;
import com.tony.blog.server.service.web.BlogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: BlogController
 * @Auther: 皮蛋布丁
 * @Date: 2021/09/29/10:05
 * @Description: 中台博文管理
 */
@RestController
@RequestMapping("sys/blog")
public class BlogController extends AbstractController {

    @Autowired
    private BlogService blogService;

    /**
    * @Description: list 获取博文列表(分页、模糊)
    * @Param: [paramMap]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/29 10:08
    */
    @RequestMapping("/list")
    public BaseResponse list(@RequestParam Map<String,Object> paramMap) {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        Map<String,Object> resMap = Maps.newHashMap();
        try {
            log.info("博文管理分页列表模糊查询的条件为：{}",paramMap);
            PageUtil page = blogService.queryPageAdmin(paramMap);
            resMap.put("page",page);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        response.setData(resMap);
        return response;
    }

    /**
    * @Description: delete 后台删除博文
    * @Param: [ids]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/29 11:14
    */
    @LogAnnotation("后台删除博文")
    @RequestMapping(value = "/delete",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresPermissions("blog:info:delete")
    public BaseResponse delete(@RequestBody Integer[] ids) {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info("后台删除博文接收的参数为：{}", Arrays.asList(ids));
            blogService.deleteEntity(Arrays.asList(ids));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: active 后台批量屏蔽/恢复博文
    * @Param: [ids]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/29 11:40
    */
    @LogAnnotation("后台批量屏蔽/恢复博文")
    @RequestMapping("active/unActive")
    @RequiresPermissions("blog:info:delete")
    public BaseResponse active(@RequestBody Integer[] ids) {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info("后台批量屏蔽/恢复博文接收的参数id为：{}",Arrays.asList(ids));
            blogService.activeEntity(Arrays.asList(ids));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }


}
